﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using GameStudio.IDE.Common.Events;
//using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.ServiceLocation;
using Utils.EventAggregator;
using GameStudio.CodeGeneration;
using GameStudio.IDE.Common;

namespace GameStudio.IDE.Controls
{
    public class OpenItemControl:Control
    {
        public OpenItemControl()
        {
            // Configure open file dialog box
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = "Document"; // Default file name
            dlg.DefaultExt = ".cs"; // Default file extension
            dlg.Filter = "Model (.txt)|*.txt| Class (.cs)|*.cs"; // Filter files by extension

            // Show open file dialog box
            Nullable<bool> result = dlg.ShowDialog();

            // Process open file dialog box results
            if (result == true)
            {
                // Open document
                string filename = dlg.FileName;
                //dlg.OpenFile();
                var eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
                eventAggregator.GetEvent<OpenItemEvent>().Publish(filename);

                AddItemToArch(filename);

                //after open item -> generate mesh
                OpenMesh(filename);
                GenerateMesh();
            }         
        }

        private void AddItemToArch(string filename)
        {

        }

        private void GenerateMesh()
        {
            GenerateMeshClass _meshClass = new GenerateMeshClass();

            _meshClass.AddVertices(FillMesh._mesh[0].VertexBuffer);
            _meshClass.AddColors(FillMesh.MyColors.ToArray());
            _meshClass.Generate();
        }

        private void OpenMesh(string filename)
        {
            FillMesh mesh = new FillMesh();
            mesh.ResetMeshes();
            mesh.ReadXmlImage(filename);
            mesh.AssignObjectToUser();
        }
        
    }
}
